//引入express模块
const express = require("express");
//引入与数据库相关的模块
const pool = require("../pool.js");
//创建路由器对象(固定语法)
const router = express.Router();

//往该路由器中添加多个子路由器

// 查询商品分类数据
router.get("/sam", (req, res, next) => {
  let sql = "select * from sam_com;";
  pool.query(sql, (err, data) => {
    if (err) {
      next(err);
      return;
    }
    res.send({
      code: 200,
      msg: "返回所有数据",
      data: data,
    });
  });
});

// 查询商品数据
router.get("/family", (req, res, next) => {
  let sid = req.query.sid;
  let fid = 1 + 6 * (sid - 1);
  let sql = "select * from sam_com_family where com_id=?";
  pool.query(sql, [sid], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    sql =
      "select f.*,c.cid,c.c_title,c.c_name,c.c_price,c.c_pic from sam_com_family f join sam_commodity c on (f.fid=c.family_id and fid=?)";
    pool.query(sql, [fid], (err, data) => {
      if (err) {
        next(err);
        return;
      }
      res.send({
        code: 200,
        msg: "返回所有数据",
        data: data,
        result: result,
      });
    });
  });
});

//查询小分类详情
router.get("/comfiy", (req, res, next) => {
  let family_id = req.query.fid;
  let sql =
    "select c.cid,c.c_title,c.c_name,c.c_price,c.c_pic from sam_commodity c where family_id=?";
  pool.query(sql, [family_id], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    res.send({
      code: 200,
      msg: "返回所有数据",
      data: data,
    });
  });
});
// 查询单一商品详情数据
router.get("/details", (req, res, next) => {
  let cid = req.query.cid;
  let sql = "select * from sam_commodity where cid=?";
  pool.query(sql, [cid], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    sql = "select * from sam_com_pic where com_id=?";
    pool.query(sql, [cid], (err, data) => {
      if (err) {
        next(err);
        return;
      }
      res.send({
        code: 200,
        msg: "返回所有数据",
        data: data,
        result: result,
      });
    });
  });
});

// 添加商品到购物车
router.post("/shopcar", (req, res, next) => {
  // 获取前台传递的参数保存到数组obj中
  let pid = req.body.pid;
  let shop_id = req.body.shop_id;
  let shoppic = req.body.shoppic;
  let shopname = req.body.shopname;
  let shopprice = req.body.shopprice;
  let count = req.body.count;
  let sql = "insert into sam_shopcar values(null,?,?,?,?,?,?)";
  pool.query(
    sql,
    [pid, shop_id, shoppic, shopname, shopprice, count],
    (err, data) => {
      if (err) {
        next(err);
        return;
      }
      res.send({ message: "ok", code: 200 });
    }
  );
});
// 查询购物车数据
router.get("/shop", (req, res, next) => {
  // 获取用户的id
  let pid = req.query.uid;
  // sql命令
  let sql = "select *from sam_shopcar where pid=?";
  pool.query(sql, [pid], (err, data) => {
    if (err) {
      next(err);
      return;
    }
    res.send({ message: "ok", code: 200, data: data });
  });
});

//查询所有商品数据的接口
router.get("/goods", (req, res, next) => {
  // 获取客户端传递的page参数
  let page = req.query.page ? req.query.page : 1;

  // 存储每页显示的记录数
  let pagesize = 20;

  // 通过公式来计算从第几条记录开始返回
  let offset = (page - 1) * pagesize;

  // 用于存储获取到的总记录数
  let rowcount;

  // 获取以下所有商品的数据的总数
  let sql = "SELECT COUNT(cid) AS count FROM sam_commodity;";
  pool.query(sql, (err, data) => {
    if (err) {
      next(err);
      return;
    }
    // 将获取到总记录数赋给rowcount变量
    rowcount = data[0].count;
    // 根据总记录数和每页显示的记录数来计算总页数
    let pagecount = Math.ceil(rowcount / pagesize);

    // 查询SQL语句
    sql = "SELECT cid,c_name,c_price,c_pic FROM sam_commodity LIMIT ?,?";
    // 执行SQL
    pool.query(sql, [offset, pagesize], (err, data) => {
      if (err) {
        next(err);
        return;
      }
      res.send({ message: "ok", code: 200, data: data, pagecount: pagecount });
    });
  });
});

// 推荐接口
router.get("/tuijian", (req, res, next) => {
  // 获取客户端传递的page参数
  let page = req.query.page ? req.query.page : 1;

  // 存储每页显示的记录数
  let pagesize = 6;

  // 通过公式来计算从第几条记录开始返回
  let offset = (page - 1) * pagesize;

  // 用于存储获取到的总记录数
  let rowcount;

  // 获取以下所有商品的数据的总数
  let sql = "SELECT COUNT(cid) AS count FROM sam_commodity;";
  pool.query(sql, (err, data) => {
    if (err) {
      next(err);
      return;
    }
    // 将获取到总记录数赋给rowcount变量
    rowcount = data[0].count;
    // 根据总记录数和每页显示的记录数来计算总页数
    let pagecount = Math.ceil(rowcount / pagesize);

    // 查询SQL语句
    sql = "SELECT cid,c_name,c_price,c_pic FROM sam_commodity LIMIT ?,?";
    // 执行SQL
    pool.query(sql, [offset, pagesize], (err, data) => {
      if (err) {
        next(err);
        return;
      }
      res.send({ message: "ok", code: 200, data: data, pagecount: pagecount });
    });
  });
});
module.exports = router;
